﻿using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.Config;
using YouHP.BLL;
using YouHP.Model;

namespace YouHP.Pages
{
    public class Register : BasePage
    {
        protected string agree = "";
        protected string createUser = "";
        PatientRecordInfo patientRecord;
        protected const int DeseaseOtherID = 10;

        protected override void ShowPage()
        {
            base.ShowPage();

            agree = RequestTools.GetQueryString("agree");
            createUser = RequestTools.GetQueryString("createuser");

            if (IsPost && createUser != "")
            {
                HandleRegister();
            }
        }

        private void HandleRegister()
        {
            bool regsucc  = false;
            bool addDeseaseRecord = true;
            UserData.UserName = RequestTools.GetFormString("username");
            UserData.Email = RequestTools.GetFormString("email");
            UserData.Password = Utils.MD5(RequestTools.GetFormString("password"), 16);
            UserData.UserTypeId = Utils.StrToInt(RequestTools.GetFormString("userType"), 1);
            string pwd = RequestTools.GetFormString("password");
            if (UserData.UserTypeId == Constants.USER_PATIENT)
            {
                UserData.UserTypeName = "病人";
            }
            else if (UserData.UserTypeId == Constants.USER_DOCTOR)
            {
                UserData.UserTypeName = "医生";
            }
            patientRecord = ValidatePatientRecord();
            if ((patientRecord.DiseaseId != 0) && (patientRecord.DiseaseId != DeseaseOtherID))
            {
                addDeseaseRecord = PatientBLL.Instance.AddPatientRecord(patientRecord);
            }
            try
            {
                if (UserBLL.Instance.CreateNewUser(UserData, pwd) && addDeseaseRecord) 
                {
                    //显示提示用户从邮箱激活账号信息,并跳转到下一个页面
                    this.AddMsgLine("注册激活码已经发送到您的邮箱  " + UserData.Email);
                    this.AddMsgLine("请从邮箱中激活您的账号！");
                    regsucc = true;
                }
                else 
                { 
                    /*处理失败*/
                    this.AddErrLine("注册失败");
                }
            }
            catch(Exception e)
            {
                this.AddErrLine(e.ToString());
            }

            if (regsucc == false)
            {
                this.AddErrLine("注册失败");
            }
        }

        private PatientRecordInfo ValidatePatientRecord()
        {
            PatientRecordInfo patientRecordInfo = new PatientRecordInfo();
            string diseaseIdString = RequestTools.GetString("dszid");
            string diseaseName = RequestTools.GetString("dszname");
            int diseaseId = 0;
            if (!string.IsNullOrEmpty(diseaseIdString))
            {
                try
                {
                    diseaseId = Convert.ToInt32(diseaseIdString);
                }
                catch (Exception )
                { }
            }
            if (diseaseId != 0)
            {
                patientRecordInfo.DiseaseId = diseaseId;
                if (!string.IsNullOrEmpty(diseaseName))
                {
                    patientRecordInfo.DiseaseName = diseaseName;
                }
            }
            patientRecord.StartDate = DateTime.Now;
            patientRecord.ConfirmDate = DateTime.Now;
            return patientRecordInfo;
        }
    }
}
